import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class Main {
    public static void main(String[] args) {

        Main main = new Main();
        //两数之和
        int[] nums = {2, 7, 11, 15};
        System.out.println("两数之和：" + Arrays.toString(main.twoSum(nums, 9)));

        //字母异位词分组
        String[] str = {"eat", "tea", "tan", "ate", "nat", "bat"};
        System.out.println("字母异位词排序：" + Collections.singletonList(main.groupAnagrams(str)));

        Stream<String> stream = Arrays.stream(str);
    }

    /**
     * 两数之和
     * @param nums 整型数组
     * @param target 给定的和
     * @return 数组中某两个数之和为target的元素下标
     */
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[]{map.get(target - nums[i]), i};
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        return new ArrayList<>(Arrays.stream(strs)
                .collect(Collectors.groupingBy(str -> {
                    // 返回 str 排序后的结果。
                    // 按排序后的结果来grouping by，算子类似于 sql 里的 group by。
                    char[] array = str.toCharArray();
                    Arrays.sort(array);
                    return new String(array);
                })).values());
    }

}